//
//  FunTuiSuanHeaderView.m
//  心水宝典
//
//  Created by zr on 2018/8/29.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "FunTuiSuanHeaderView.h"

@interface FunTuiSuanHeaderView ()

@property (nonatomic, strong)UIButton * topImageButton;
@property (nonatomic, strong)UIButton * leftImageButton;
@property (nonatomic, strong)UIButton * rightImageButton;
@property (nonatomic, strong)UIImageView * hotImageView;
@property (nonatomic, strong)UIImageView * tuiImageView;
@property (nonatomic, strong)UIImageView * tuiTwoImageView;
@property (nonatomic, strong)UILabel * oneTitleLabel;
@property (nonatomic, strong)UILabel * twoTitleLabel;
@property (nonatomic, strong)UILabel * threeTitleLabel;
@property (nonatomic, strong)UIButton * oneSeeButton;
@property (nonatomic, strong)UIButton * oneCommitButton;
@property (nonatomic, strong)UIButton * twoSeeButton;
@property (nonatomic, strong)UIButton * twoCommitButton;
@property (nonatomic, strong)UIButton * threeSeeButton;
@property (nonatomic, strong)UIButton * threeCommitButton;
@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UILabel * tuiLabel;
@property (nonatomic, weak)UILabel * tuiTwoLabel;

@end

@implementation FunTuiSuanHeaderView


- (void)setFunFirstArray:(NSArray<FunModel *> *)funFirstArray{
    
    _funFirstArray = funFirstArray;
    
    FunModel * oneModel = funFirstArray.firstObject;
    
    NSURL * oneUrl = [NSURL URLWithString:oneModel.img];
    [self.topImageButton sd_setImageWithURL:oneUrl forState:UIControlStateNormal];
    self.oneTitleLabel.text = oneModel.title;
    [self.oneSeeButton setTitle:oneModel.hits forState:UIControlStateNormal];
    [self.oneCommitButton setTitle:oneModel.comment forState:UIControlStateNormal];
    
    if (funFirstArray.count > 0) {
        
        FunModel * twoModel = funFirstArray[1];
        
        NSURL * twoUrl = [NSURL URLWithString:twoModel.img];
        [self.leftImageButton sd_setImageWithURL:twoUrl forState:UIControlStateNormal];
        self.twoTitleLabel.text = twoModel.title;
        [self.twoSeeButton setTitle:twoModel.hits forState:UIControlStateNormal];
        [self.twoCommitButton setTitle:twoModel.comment forState:UIControlStateNormal];

    }
    
    FunModel * threeModel = funFirstArray.lastObject;
    
    NSURL * threeUrl = [NSURL URLWithString:threeModel.img];
    [self.rightImageButton sd_setImageWithURL:threeUrl forState:UIControlStateNormal];
    self.threeTitleLabel.text = threeModel.title;
    [self.threeSeeButton setTitle:threeModel.hits forState:UIControlStateNormal];
    [self.threeCommitButton setTitle:threeModel.comment forState:UIControlStateNormal];
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor lightGrayColor];
    
    self.topImageButton = [self setupButtonWithImage:@""];
    self.topImageButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.topImageButton];
    self.leftImageButton = [self setupButtonWithImage:@""];
    self.leftImageButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.leftImageButton];
    self.rightImageButton = [self setupButtonWithImage:@""];
    self.rightImageButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.rightImageButton];
    
    self.hotImageView = [self setupImageViewWith:@"hot"];
    [self addSubview:self.hotImageView];
    self.tuiImageView = [self setupImageViewWith:@"tuijian"];
    [self.leftImageButton addSubview:self.tuiImageView];
    self.tuiTwoImageView = [self setupImageViewWith:@"tuijian"];
    [self.rightImageButton addSubview:self.tuiTwoImageView];
    
    self.oneTitleLabel = [self setupLabelWithTitle:@"测一测什么时候才能碰见能陪伴一生的他（她）？"];
    [self addSubview:self.oneTitleLabel];
    self.twoTitleLabel = [self setupLabelWithTitle:@"测一测你家的宠物上辈子是你的谁？"];
    self.twoTitleLabel.numberOfLines = 2;
    [self addSubview:self.twoTitleLabel];
    self.threeTitleLabel = [self setupLabelWithTitle:@"测一测你上辈子是皇帝的哪个妃子"];
    self.threeTitleLabel.numberOfLines = 2;
    [self addSubview:self.threeTitleLabel];
    
    self.oneSeeButton = [self setupButtonWithTitle:@"  2034" WithType:@"see"];
    [self addSubview:self.oneSeeButton];
    self.twoSeeButton = [self setupButtonWithTitle:@"  2034" WithType:@"see"];
    [self addSubview:self.twoSeeButton];
    self.threeSeeButton = [self setupButtonWithTitle:@"  2034" WithType:@"see"];
    [self addSubview:self.threeSeeButton];
    
    self.oneCommitButton = [self setupButtonWithTitle:@"  2034" WithType:@"commit"];
    [self addSubview:self.oneCommitButton];
    self.twoCommitButton = [self setupButtonWithTitle:@"  2034" WithType:@"commit"];
    [self addSubview:self.twoCommitButton];
    self.threeCommitButton = [self setupButtonWithTitle:@"  2034" WithType:@"commit"];
    [self addSubview:self.threeCommitButton];
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.backView = backView;
    
    [self addSubview:backView];
    
    UILabel * tuiLabel = [UILabel new];
    
    tuiLabel.textColor = WHITE_COLOR;
    tuiLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    tuiLabel.text = @"推荐";
    
    self.tuiLabel = tuiLabel;
    
    [self.tuiImageView addSubview:self.tuiLabel];
    
    
    UILabel * tuiTwoLabel = [UILabel new];
    
    tuiTwoLabel.textColor = WHITE_COLOR;
    tuiTwoLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    tuiTwoLabel.text = @"推荐";
    
    self.tuiTwoLabel = tuiTwoLabel;
    
    [self.tuiTwoImageView addSubview:self.tuiTwoLabel];
    
}


- (void)layoutSubviews{
    
    [self.topImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self);
        make.height.mas_equalTo(140 * SCREENH6);
    }];
    
    [self.hotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.mas_equalTo(self.topImageButton);
        make.width.mas_equalTo(53 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
    }];
    
    [self.oneTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
         make.trailing.mas_equalTo(self).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.topImageButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.oneCommitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.oneTitleLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.oneSeeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.oneCommitButton.mas_leading).offset(-8 * SCREENH6);
        make.centerY.mas_equalTo(self.oneCommitButton);
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(10 * SCREENH6);
        make.top.mas_equalTo(self.oneSeeButton.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.leftImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneTitleLabel);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(165 * SCREENH6);
        make.height.mas_equalTo(105 * SCREENH6);
    }];
    
    [self.tuiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.leftImageButton);
        make.top.mas_equalTo(self.leftImageButton).offset(8 * SCREENH6);
        make.width.mas_equalTo(48 * SCREENH6);
        make.height.mas_equalTo(19 * SCREENH6);
    }];
    
    [self.tuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.tuiImageView);
        make.centerX.mas_equalTo(self.tuiImageView);
    }];
    
    [self.twoTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.leftImageButton);
        make.top.mas_equalTo(self.leftImageButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.twoSeeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoTitleLabel);
        make.top.mas_equalTo(self.twoTitleLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.twoCommitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoSeeButton.mas_trailing).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.twoSeeButton);
    }];
    
    [self.rightImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.oneCommitButton);
        make.centerY.width.height.mas_equalTo(self.leftImageButton);
    }];
    
    [self.tuiTwoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.rightImageButton);
        make.centerY.width.height.mas_equalTo(self.tuiImageView);
    }];
    
    [self.tuiTwoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.tuiTwoImageView);
        make.centerX.mas_equalTo(self.tuiTwoImageView);
    }];
    
    [self.threeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.rightImageButton);
        make.top.mas_equalTo(self.rightImageButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.threeSeeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threeTitleLabel);
        make.top.mas_equalTo(self.threeTitleLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.threeCommitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threeSeeButton.mas_trailing).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.threeSeeButton);
    }];
}


- (UIButton *)setupButtonWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UIButton * button = [UIButton new];
    
    NSString * image;
    if ([type isEqualToString:@"see"]) {
        
        image = @"liulanliang";
    }else{
        
        image = @"pinglun";
    }
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:ImageNamed(image) forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    return button;
}


- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
        
        label.textColor = BLACK_COLOR;
        label.font = [UIFont systemFontOfSize:15 * SCREENH6];

    label.text = title;
    
    return label;
}

- (UIImageView *)setupImageViewWith:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = ImageNamed(image);
    
    return imageView;
}


- (UIButton *)setupButtonWithImage:(NSString *)image{
    
    UIButton * button = [UIButton new];
    
    [button setImage:ImageNamed(image) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickImageButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void)clickImageButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.topImageButton) {
        
        type = @"0";
    }else if (button == self.leftImageButton){
        
        type = @"1";
    }else if (button == self.rightImageButton){
        
        type = @"2";
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToFunInfo" object:nil userInfo:dict];
}


@end
